//
//  RXDefinedCell.m
//  DefinedEqualHeightTB
//
//  Created by Ruoxin on 17/3/15.
//  Copyright © 2017年 Ruoxin. All rights reserved.
//

#import "RXDefinedCell.h"
#import "Masonry.h"

#define KInterval 10

@interface RXDefinedCell ()

@property (nonatomic, weak)UIImageView *iconImgView;
@property (nonatomic, weak)UILabel *titleLabel;
@property (nonatomic, weak)UILabel *buyCountLabel;
@property (nonatomic, weak)UILabel *priceLabel;

@end

@implementation RXDefinedCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        /** 图片 **/
        UIImageView *imgView = [[UIImageView alloc] init];
        // 在这里是无法设置的
//        imgView.image = [UIImage imageNamed:_dataModel.icon];
        [self.contentView addSubview:imgView];
        self.iconImgView = imgView;
        
        /** 标题 **/
        UILabel *titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        /** 购买数 **/
        UILabel *buyCountLabel = [[UILabel alloc] init];
        buyCountLabel.textAlignment = NSTextAlignmentRight;
        buyCountLabel.font = [UIFont systemFontOfSize:13];
        buyCountLabel.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:buyCountLabel];
        self.buyCountLabel = buyCountLabel;
        
        /** 价格 **/
        UILabel *priceLabel = [[UILabel alloc] init];
        priceLabel.font = [UIFont systemFontOfSize:15];
        priceLabel.textColor = [UIColor orangeColor];
        [self.contentView addSubview:priceLabel];
        self.priceLabel = priceLabel;

    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    /** 图片 **/
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(KInterval);
        make.bottom.offset(-KInterval);
        make.width.mas_equalTo(100);
    }];
    
    /** 标题 **/
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImgView.mas_top);
        make.left.mas_equalTo(self.iconImgView.mas_right).offset(KInterval);
        make.right.offset(-KInterval);
        make.height.mas_equalTo(20);
    }];
    
     /** 价格 **/
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImgView.mas_right).offset(KInterval);
        make.bottom.mas_equalTo(self.iconImgView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(50, 20));
    }];
    
    /** 购买数 **/
    [self.buyCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.offset(-KInterval);
        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];
}

// 将整个 Model 传过来，无需将接口暴露出去
// 设置数据
- (void)setDataModel:(RXDataModel *)dataModel {
    _dataModel = dataModel;
    self.iconImgView.image = [UIImage imageNamed:dataModel.icon];
    self.titleLabel.text = dataModel.title;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@", dataModel.price] ;
    self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", dataModel.buyCount];
}




@end
